package 设计模式.责任链模式;

import java.util.ArrayList;
import java.util.List;

public class RealnameResponsibilityPattern {

    static class ChannelChain{

        private List<Channel> elementList;

        private Integer              index = 0;

        private final static int     MAX_LOOP = 2;

        public ChannelChain(List<Channel> elementList) {
            this.elementList = elementList;
        }

        public boolean proceed() {
            //最大执行次数
            int size = elementList.size() > MAX_LOOP ? MAX_LOOP : elementList.size();
            boolean isSuccess = elementList.get(index).isSuccess();
            if (isSuccess || (index + 1) >= size) {
                //清除所有的临时变量
                return true;
            } else {
                index++;
                return this.proceed();
            }
        }
    }

    static interface Channel{
        boolean isSuccess();
    }

    static class ALiChannel implements Channel{

        @Override
        public boolean isSuccess() {
            System.out.println("Ali Channel false");
            return false;
        }
    }

    static class BaiduChannel implements Channel{

        @Override
        public boolean isSuccess() {
            System.out.println("Baidu Channel true");
            return true;
        }
    }

    public static void main(String[] args) {
        List<Channel> channelList = new ArrayList<>();
        channelList.add(new ALiChannel());
        channelList.add(new BaiduChannel());

        boolean isSucc = new ChannelChain(channelList).proceed();
        System.out.println("result:"+isSucc);
    }
}